//
//  recommendHelper.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "recommendHelper.h"
#import "ScrollViewModel.h"
#import "recommendModel.h"
#import "rankController.h"
#import "sectionModel.h"
#import "TopModel.h"

#define RecommendURL @"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=2&contentType=album&device=android&scale=2&version=4.3.20.2"

#define RankURL @"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=android&pageId=1&pageSize=20&status=0&tagName=%E6%A6%9C%E5%8D%95%7C%E6%8E%92%E8%A1%8C"

#define RankURL1 @"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=android&pageId="

#define RankURL2 @"&pageSize=20&status=0&tagName=%E6%A6%9C%E5%8D%95%7C%E6%8E%92%E8%A1%8C"

@interface recommendHelper ()

@property(nonatomic,strong)NSMutableArray *recommendDataArray;
@property(nonatomic,strong)NSMutableArray *scrollViewDataArray;
@property(nonatomic,strong)NSMutableArray *topDataArray;

// 排行的属性
@property(nonatomic,strong)NSMutableArray *rankDataArray;

@property(nonatomic,strong)rankController *rankVC;

@end


@implementation recommendHelper

+(recommendHelper*)sharedInstancetype
{
    static recommendHelper *beautiful = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        beautiful = [[recommendHelper alloc]init];
    });
    return beautiful;

}



-(void)downLoadDataFinished:(void(^)())result2 
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSURLSession *session = [NSURLSession sharedSession];
    
        // 拿到一个 url
         NSURL *url = [NSURL URLWithString:RecommendURL];
        
        NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
           
            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//            NSLog(@"%@",dict);
           
            NSDictionary *recommendDict = result[@"categoryContents"];
            NSMutableArray *listArray = recommendDict[@"list"];
            
            //  scrollView 的 数据
            
            NSDictionary *zeroDict = listArray.firstObject;
            
            NSArray *smallListArray = zeroDict[@"list"];
                   TopModel *model = [[TopModel alloc]init];
            [model setValuesForKeysWithDictionary:smallListArray.firstObject];
                   [self.topDataArray addObject:model];
                //   NSLog(@"--------------%@",self.topDataArray);
            
            [listArray removeObjectAtIndex:0];
          //  NSLog(@"数组元素个数 = %ld",listArray.count);
            self.sectionArray = [NSMutableArray array];
               for (NSDictionary *dict in listArray)
            {
                sectionModel *model = [[sectionModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.sectionArray addObject:model];
             //   NSLog(@"sectionarray的个数为 ： %ld",self.sectionArray.count);
                
                
            //    NSMutableArray *smallArray = [NSMutableArray array];
//                self.listDataArray = model.list;
                for (NSDictionary *listDict in model.list)
                {
                recommendModel *model = [[recommendModel alloc]init];
                [model setValuesForKeysWithDictionary:listDict];
                [self.recommendDataArray addObject:model];
                    
               // [smallArray addObject:model];
            
                }
              //  [self.recommendDataArray addObject:smallArray];
               
            }
            
            NSDictionary *scrollViewDict = result[@"focusImages"];
            
            NSArray *scrollArray = scrollViewDict[@"list"];
            
            for (NSDictionary *dict in scrollArray)
            {
                ScrollViewModel *model = [[ScrollViewModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.scrollViewDataArray addObject:model];
              //  NSLog(@"%@",model.pic);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                result2();

             });
        }];
        
        [task resume];
    });
   
    
}


-(void)downLoadrankDataFinished:(void(^)())result3 withIndex:(NSInteger)index
{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSURLSession *session = [NSURLSession sharedSession];
        
        //   NSLog(@"=========%ld",index);

        
        NSString *url1 = [NSString stringWithFormat:@"%@%ld%@",RankURL1,index + 1,RankURL2];
        NSLog(@"网址变变变 == %@",url1);
        
        
        // 拿到一个 url
        NSURL *url = [NSURL URLWithString:url1];
        
        NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
          NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSArray *rankArray = result[@"list"];
        //    NSLog(@"=-=-=-=-=-=-=-%@",rankArray);
            for (NSDictionary *dict in rankArray)
            {
                recommendModel *model = [[recommendModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                
                [self.rankDataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                result3();
                
            });
        }];
        
        [task resume];
    });
   
            
}



-(NSMutableArray *)recommendDataArray
{
    if (!_recommendDataArray)
    {
        _recommendDataArray = [NSMutableArray array];
    }
    return _recommendDataArray;
}

-(NSMutableArray *)scrollViewDataArray
{
    if (!_scrollViewDataArray)
    {
        _scrollViewDataArray = [NSMutableArray array];
    }
    return _scrollViewDataArray;
}

-(NSMutableArray *)topDataArray
{
    if (!_topDataArray)
    {
        _topDataArray = [NSMutableArray array];
    }
    return _topDataArray;
}

-(NSArray *)recommendArray
{
    return [self.recommendDataArray copy];
}

-(NSArray *)scrollArray
{
    
    return [self.scrollViewDataArray copy];
    
}

-(NSArray *)topArray
{
    return [self.topDataArray copy];
}

-(NSMutableArray *)rankDataArray
{
    if (!_rankDataArray)
    {
        _rankDataArray = [NSMutableArray array];
    }
    return _rankDataArray;
}

-(NSArray*)rankArray
{
    return [self.rankDataArray copy];
}




@end
